package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.project.RCSaleCommissionTotalSettlement;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.RCSalesAssignmentStatus;
import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.proxy.RCSalesAssignmentProxy;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCSALESASSIGNMENT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcsalesassignment")
public class RCSalesAssignmentImpl extends BaseModelObject implements RCSalesAssignment {
  public RCSalesAssignmentImpl() {
    super();
  }
  
  private RCSalesAssignmentStatus Status;
  
  public RCSalesAssignmentStatus getStatus() {
    return this.Status;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    Status=RCSalesAssignmentStatus.onJob;
  }
  
  public void start() {
    if(Status==null){
    	_start();
    	getModelObjectLogService().createLog(this,"Status","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.project.events.RCSalesAssignmentEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void _leave(final Date leaveDate) {
    
    this.leaveDate = leaveDate;
    Status=RCSalesAssignmentStatus.leaveJob;
  }
  
  public void leave(final Date leaveDate) {
    if(Status==RCSalesAssignmentStatus.onJob){
    	_leave(leaveDate);
    	getModelObjectLogService().createLog(this,"Status","leave");
    	getBeanFactory().getBean(com.shrct.businessprocess.project.events.RCSalesAssignmentEventsManager.class).fireLeave(this,leaveDate);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  public Boolean isOnJob() {
    return com.shrct.businessprocess.project.RCSalesAssignmentStatus.onJob==Status;
  }
  
  public Boolean isLeaveJob() {
    return com.shrct.businessprocess.project.RCSalesAssignmentStatus.leaveJob==Status;
  }
  
  @JoinColumn(name = "saler_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee saler;
  
  public RCEmployee getSaler() {
    return this.saler;
  }
  
  public RCSalesAssignment setSaler(final RCEmployee saler) {
    this.saler = saler;
    return this;			
    
  }
  
  private Date startDate;
  
  public Date getStartDate() {
    return this.startDate;
  }
  
  public RCSalesAssignment setStartDate(final Date startDate) {
    this.startDate = startDate;
    return this;			
    
  }
  
  private Date leaveDate;
  
  public Date getLeaveDate() {
    return this.leaveDate;
  }
  
  public RCSalesAssignment setLeaveDate(final Date leaveDate) {
    this.leaveDate = leaveDate;
    return this;			
    
  }
  
  public Collection<RCSalesCommissionYearConfig> getCommissionConfigs() {
    IGenericQuery<RCSalesCommissionYearConfig> query=getObjectFactory().createGenericQuery(RCSalesCommissionYearConfig.class,"select e from com.shrct.businessprocess.project.impl.RCSalesCommissionYearConfigImpl as e   where ((e.saler) = (:this)) and  e.active = 1      ").setParameter("this",this);
    Collection<RCSalesCommissionYearConfig> _list = query.list();
    return _list;
  }
  
  public Collection<RCSaleCommissionTotalSettlement> getCommissions() {
    IGenericQuery<RCSaleCommissionTotalSettlement> query=getObjectFactory().createGenericQuery(RCSaleCommissionTotalSettlement.class,"select e from com.shrct.businessprocess.project.impl.RCSaleCommissionTotalSettlementImpl as e   where ((e.saler.saler) = (:saler)) and  e.active = 1  order by e.date DESC    ").setParameter("saler",saler);
    Collection<RCSaleCommissionTotalSettlement> _list = query.list();
    return _list;
  }
  
  public String getEntityName() {
    String _entityName = this.saler.getEntityName();
    return _entityName;
  }
  
  public void update(final Procedure1<? super RCSalesAssignment> updater) {
    updater.apply(this);
    this.checkValid();
  }
  
  public void checkValid() {
    boolean _notEquals = (!Objects.equal(this.saler, null));
    Validates.Assert(_notEquals, "\u9500\u552E\u5458\u5DE5\u4E0D\u80FD\u4E3A\u7A7A");
    boolean _notEquals_1 = (!Objects.equal(this.startDate, null));
    Validates.Assert(_notEquals_1, "\u5165\u804C\u65E5\u671F\u4E0D\u80FD\u4E3A\u7A7A");
    IGenericQuery<Long> query=getObjectFactory().createGenericQuery(Long.class,"select count(*) from com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl as e   where ((e.saler) = (:saler)) and  e.active = 1      ").setParameter("saler",saler);
    Long _uniqueResult = query.uniqueResult();
    boolean _equals = ((_uniqueResult).longValue() == 1);
    Validates.Assert(_equals, "\u9500\u552E\u91CD\u590D");
  }
  
  public static RCSalesAssignment create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCSalesAssignment rCSalesAssignment = new com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl(
    );
    objectFactory.create(rCSalesAssignment);
    return rCSalesAssignment;			
    
  }
  
  public static RCSalesAssignment createRCSalesAssignmentImpl(final IObjectFactory objectFactory, final Procedure1<RCSalesAssignment> init) {
    com.shrct.businessprocess.project.RCSalesAssignment rCSalesAssignment = new com.shrct.businessprocess.project.impl.RCSalesAssignmentImpl(
    );
    objectFactory.create(rCSalesAssignment);
    init.apply(rCSalesAssignment);
    
    return rCSalesAssignment;			
    
  }
  
  public RCSalesAssignmentProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCSalesAssignmentProxy proxy = new com.shrct.businessprocess.project.proxy.RCSalesAssignmentProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setStatus(this.getStatus().name());
    return proxy;
    
  }
}
